package pucp.carpooling;


import com.actionbarsherlock.app.ActionBar;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import pucp.carpooling.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MapaActivityViaje extends Activity {
	// Google Map
	private GoogleMap googleMap;
	private final LatLng SUBETEPUCP = new LatLng(-12.069284, -77.078518);
	LatLng PuntoDestino;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		android.app.ActionBar ab = getActionBar();
		// ab.setDisplayHomeAsUpEnabled(true);
		ab.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP,
				ActionBar.DISPLAY_HOME_AS_UP);
		
		
		setContentView(R.layout.activity_mapa);
		Intent i = getIntent();
		// latitude and longitude
		try { 
			// Loading map
			initilizeMap();
			googleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)).position(SUBETEPUCP).title("Paradero PUCP"));
			LatLng coordinate = SUBETEPUCP;
			CameraUpdate myLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 12);
			
			PuntoDestino = new LatLng(i.getExtras().getFloat("latend"),i.getExtras().getFloat("lngend"));
			googleMap.addMarker(new MarkerOptions()
			.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))
			.position(PuntoDestino)
			.title("Destino Final"));
			
			
			
			
			googleMap.animateCamera(myLocation);
			googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
			googleMap.setMyLocationEnabled(true);
			googleMap.getUiSettings().setZoomControlsEnabled(true);
			googleMap.getUiSettings().setMyLocationButtonEnabled(true);
			googleMap.getUiSettings().setCompassEnabled(true);
			googleMap.getUiSettings().setRotateGesturesEnabled(true);
			googleMap.getUiSettings().setZoomGesturesEnabled(true);
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * function to load map. If map is not created it will create it for you
	 * */
	private void initilizeMap() {
		if (googleMap == null) {
			googleMap = ((MapFragment) getFragmentManager().findFragmentById(
					R.id.map)).getMap();
			
			
			// check if map is created successfully or not
			if (googleMap == null) {
				Toast.makeText(getApplicationContext(),
						"Sorry! unable to create maps", Toast.LENGTH_SHORT)
						.show();
			}
		}
	}

	public boolean onOptionsItemSelected(android.view.MenuItem item) {
		switch (item.getItemId()) {

		case android.R.id.home:
			finish();
			return true;

		default:
			return super.onOptionsItemSelected(item);
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		initilizeMap();
	}


	
}
